﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.Presentation.Web.Models;
using ProjetoComperio.SystemFramework.Extensions;
using ProjetoComperio.SystemFramework.Web;
using System.Web;
using System.IO;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class ContributionController : ComperioController
    {
        private ContributionBLL contributionBll;
        private ContributionItemBLL contributionItemBLL;
        private AcademicPersonBLL academicPersonBll;

        public ContributionController()
        {
            contributionBll = new ContributionBLL();
            academicPersonBll = new AcademicPersonBLL();
            contributionItemBLL = new ContributionItemBLL();
        }

        [HttpGet]
        public ActionResult AddAcademicPerson()
        {
            Session["AcademicOrganizations"] = contributionBll.RetrieveAcademicPersonContributions(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }
        [HttpPost]
        public ActionResult AddAcademicPerson(AddAcademicPerson addAcademicPerson)
        {
            if (!ModelState.IsValid)
                return View();

            AcademicPersonBLL academicPersonBll = new AcademicPersonBLL();
            academicPersonBll.RegisterAcademicPersonInContribution(addAcademicPerson.ContributionID, Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }

        [HttpGet]
        public ActionResult ListAcademicPeople()
        {
            var contributions = contributionBll.RetrieveAcademicPersonContributions(Convert.ToInt32(this.CurrentUser.PersonID));
            Session["contributions"] = contributions.ToSelectedItemList<Contribution>(ao => ao.ContributionID, ao => ao.Title);
            return View(new ListAcademicPeople());
        }
        [HttpPost]
        public ActionResult ListAcademicPeople(FormCollection form)
        {
            academicPersonBll.RetrieveAcademicPeopleInContribution(Convert.ToInt32(form["ContributionID"]));
            ListAcademicPeople listAcademicPeopleModel = new ListAcademicPeople();
            listAcademicPeopleModel.PeopleInProject = academicPersonBll.RetrieveAcademicPeopleInContribution(Convert.ToInt32(form["ContributionID"]));
            return View(listAcademicPeopleModel);

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadFile()
        {
            foreach (string inputedFile in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[inputedFile];

                if (file.ContentLength > 0)
                {
                    file.SaveAs(Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName)));
                }

                var contributionItem = new ContributionItem
                {
                    ContributionID = Convert.ToInt32(URLEncrypt.DecryptFromString(Session["id"].ToString())),
                    FileName = file.FileName,
                    FileUrl = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName)),
                    UploadDate = DateTime.Now,
                    UserName = "UserNameDefault"
                };

                contributionItemBLL.Create(contributionItem);
            }

            return Redirect(Request.UrlReferrer.ToString());
        }
    }
}
